/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.treepoke.common.properties;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author dvega
 */
public class PropertiesLoader {
    
    /**
     * Variable to be loaded when the VM starts.
     */
    public final static String PROPERTIES_FILE = "b2ctp.properties.file";
    /**
     * Singleton instance of the class
     */
    private static PropertiesLoader propsLoader = null;
    /**
     * Properties loaded
     */
    private Properties properties = null;

    /**
     * Creates a PropertiesLoader by loading a properties file.
     */
    private PropertiesLoader() throws Exception {
        loadPropertiesFile();
    }

    /**
     * Returns the singleton instance.
     *
     * @return Singleton Instance
     */
    private static PropertiesLoader getInstance() throws Exception {

        if (propsLoader == null) {
            propsLoader = new PropertiesLoader();
        }

        return propsLoader;
    }

    /**
     * Lets retrieve a property by its name.
     * @param key Key to retrieve
     * @return Key value as String
     */
    public static String getProperty(String key) {
        String value = null;
        try {
            value = getInstance().properties.getProperty(key);
        } catch (Exception e) {
            Logger.getLogger(PropertiesLoader.class.getPackage().getName()).
                    log(Level.SEVERE, "Error obteniendo propiedad: " + key, e);
        }
        return value;
    }

    /**
     * Loads the properties from the configuration file.
     */
    private void loadPropertiesFile() throws Exception {
        InputStream instr = null;

        try {
            String filePath = System.getProperties().getProperty(PROPERTIES_FILE);
            if (filePath != null) {
                Properties props = new Properties();
                instr = new FileInputStream(filePath);

                props.load(instr);
                properties = props;
            } else {
                throw new Exception("The properties file is not configured yet, You must set " + PROPERTIES_FILE + " property with the syntax -D"
                        + PROPERTIES_FILE + "=Path to your properties.");
            }
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                instr.close();
            } catch (Exception e) {
                Logger.getLogger(PropertiesLoader.class.getPackage().getName()).
                    log(Level.SEVERE, "Error cerrando archivo propiedades", e);
            }
        }
    }

    /**
     * Forces the reloading of properties file by setting to null the singleton
     * instance.
     */
    public static void forcePropertiesFileReload() {
        propsLoader = null;
    }
    
}
